import axios from 'axios'
import {notification } from 'antd';
import {createBrowserHistory} from 'history'
var history = createBrowserHistory()

//实例化axios对象
var service = axios.create({
    baseURL:'',
    timeout:10*1000,//超时时间
})
//设置请求拦截器
service.interceptors.request.use(
    (config)=>{
        config.headers.Authorization = "Bearer" +" " +localStorage.getItem('token')
        return config
    },
    (error)=>{
        return Promise.reject(error)
    },
)
//设置响应拦截器
service.interceptors.response.use(
    (config)=>{
        return config
    },
    (error)=>{
        if(error.reponse.status==401){
            notification.error({message:'登陆过期,请重新登录'})
            //删除token,跳转到登录页
            localStorage.removeItem('token')
            //跳转登录页
            history.push('/login');
            //跳路由如果路由跳转了,但是组件没刷新,就可以使用go方法
            history.go();
        }
        else if(error.reponse.status==404){
            notification.error({message:'访问路径有误'})
        }
        else if(error.reponse.status==500){
            notification.error({message:'服务器内部错误'})
        }
        return  Promise.reject(error)
    }
)
//导出
export default service